using Microsoft.EntityFrameworkCore;
using RugbyClubWpf.Models;

namespace RugbyClubWpf.Data
{
    /// <summary>
    /// Represents the database context for the Rugby Club application.
    /// </summary>
    public class RugbyClubContext : DbContext
    {
        public DbSet<Coach> Coaches { get; set; }
        public DbSet<Player> Players { get; set; }
        public DbSet<President> Presidents { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Match> Matches { get; set; }
        public DbSet<Announcement> Announcements { get; set; }

        /// <summary>
        /// Configures the database connection.
        /// </summary>
        /// <param name="optionsBuilder">The options builder.</param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Server=LAPTOP-EQJNVKJ4\\SQLSERVER2;Database=RugbyClubWpf;User=sa;Password=123456;MultipleActiveResultSets=True;TrustServerCertificate=True;");
        }

        /// <summary>
        /// Configures the model relationships and seed data.
        /// </summary>
        /// <param name="modelBuilder">The model builder.</param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Coach>()
                .HasOne(c => c.User)
                .WithOne()
                .HasForeignKey<Coach>(c => c.UserId);

            modelBuilder.Entity<Player>()
                .HasOne(p => p.User)
                .WithOne()
                .HasForeignKey<Player>(p => p.UserId);

            modelBuilder.Entity<President>()
                .HasOne(p => p.User)
                .WithOne()
                .HasForeignKey<President>(p => p.UserId);

            modelBuilder.Entity<Coach>().HasData(
                new Coach { CoachId = 1, Name = "John Doe", Email = "john.doe@example.com", PhoneNumber = "123-456-7890", UserId = 1 },
                new Coach { CoachId = 2, Name = "Jane Smith", Email = "jane.smith@example.com", PhoneNumber = "098-765-4321", UserId = 2 }
            );

            modelBuilder.Entity<Player>().HasData(
                new Player { PlayerId = 1, Name = "Tom Brown", Position = "Forward", Age = 25, UserId = 3, CoachId = 1 },
                new Player { PlayerId = 2, Name = "Sam Green", Position = "Back", Age = 22, UserId = 4, CoachId = 2 }
            );

            modelBuilder.Entity<Match>().HasData(
                new Match { MatchId = 1, Opponent = "Team A", Date = new DateTime(2023, 5, 1), Location = "Stadium 1", CoachId = 1 },
                new Match { MatchId = 2, Opponent = "Team B", Date = new DateTime(2023, 6, 1), Location = "Stadium 2", CoachId = 2 }
            );

            modelBuilder.Entity<President>().HasData(
                new President { PresidentId = 1, Name = "Alice Johnson", Email = "alice.johnson@example.com", UserId = 5 },
                new President { PresidentId = 2, Name = "Bob White", Email = "bob.white@example.com", UserId = 6 }
            );

            modelBuilder.Entity<User>().HasData(
                new User { UserId = 1, Username = "coach1", Password = User.HashPassword("password1"), Role = "Coach" },
                new User { UserId = 2, Username = "coach2", Password = User.HashPassword("password2"), Role = "Coach" },
                new User { UserId = 3, Username = "player1", Password = User.HashPassword("password1"), Role = "Player" },
                new User { UserId = 4, Username = "player2", Password = User.HashPassword("password2"), Role = "Player" },
                new User { UserId = 5, Username = "president1", Password = User.HashPassword("password1"), Role = "President" },
                new User { UserId = 6, Username = "president2", Password = User.HashPassword("password2"), Role = "President" }
            );
        }
    }
}
